package cn.lyvust.db;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LuckyDb extends JFrame {
    int lines = 0;
    int index = 0;
    Connection conn = null;
    String luckyName = null;
    List<String> names = new ArrayList<>();

    public static void main(String[] args) {
        new LuckyDb();
    }

    public LuckyDb() {
        getNames();
        this.setTitle("临科抽奖小程序");
        this.setSize(300, 200);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        //设置自定义布局,否则按钮会被干预,显示位置不符合预期
        this.setLayout(null);
        this.init();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void init() {
        JButton jbStart = new JButton("开始");
        JButton jbEnd = new JButton("结束");
        //设置按钮的位置和大小
        jbStart.setBounds(32, 107, 93, 37);
        jbEnd.setBounds(165, 107, 93, 37);
        //将按钮显示到窗体上
        this.add(jbStart);
        this.add(jbEnd);

        //设置文本显示到窗体上
        JLabel jlStart = new JLabel("开始");
        jlStart.setFont(new Font(Font.DIALOG, Font.BOLD, 30));
        jlStart.setBounds(70, 45, 120, 30);
        this.add(jlStart);

        //定义一个定时器,每100毫秒执行一次
        Timer timer = new Timer(10, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int random = 0;
                Random rand = new Random();
                if (lines <= 0) {
                    return;
                }
                random = rand.nextInt(lines);
                if (index >= names.size() && lines != 0) {
                    index = index%lines;
                }
                luckyName = names.get(index);
                jlStart.setText(luckyName);
                index += random;
            }
        });

        //按钮的响应函数/动作
        jbStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//                System.out.println("你点击了一下开始按钮");
                timer.start();
            }
        });
        //点击结束按钮的响应函数
        jbEnd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
                resetName();
            }
        });
    }

    public void resetName() {
        String sql = "update luck_info set num = ? where name = ? ;";
        // 使用PreparedStatement来预加载SQL语句
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            // 参数一：预加载的SQL语句中?的序号
            // 参数二：?的数据
            ps.setInt(1, 1);
            ps.setString(2, luckyName);
            // 执行更新操作
            int result = ps.executeUpdate();
            // 显示结果
            System.out.println("成功更改" + result + "条数据！");
            // 关闭Statement
            ps.close();
            names.clear();
            lines = 0;
            getNames();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    //读取学生的全部名单信息
    public void getNames() {
        // MySQL的JDBC连接语句
        // URL编写格式：jdbc:mysql://主机名称：连接端口/数据库的名称?参数=值
        String url = "jdbc:mysql://localhost:3306/lucky?user=root&password=123456";
        try {
            Class.forName("com.mysql.jdbc.Driver"); // 加载驱动
            conn = DriverManager.getConnection(url); // 获取数据库连接
            // 查询语句
            String sql = "SELECT * FROM luck_info where num != 1;";
            // 使用Statement来执行SQL语句
            Statement state = conn.createStatement();
            // 执行查询操作
            ResultSet rs = state.executeQuery(sql);
            // 遍历结果
            while (rs.next()) {
                // 通过字段名称获得字段值
                String id = rs.getString("id");
                String name = rs.getString("name");
                int num = rs.getInt("num");
                // 打印一条结果
                System.out.println(id + "-" + name + "-" + num);
                names.add(name);
                lines ++;
            }
            // 关闭ResultSet
            rs.close();
            // 关闭Statement
            state.close();
//            conn.close(); // 关闭数据库连接
        } catch (Exception e) {
            System.out.println("代码或数据库异常" + e);
        }
    }
}
